import * as echarts from '../../../ec-canvas/echarts.min.js';
import { getUserGrade, getUserInfo } from '../../../utils/userInfo';
let chart = null;

function initChart(canvas, width, height, dpr) {
    const { _id } = getUserGrade();
    let gradeArr = [];
    wx.cloud.callFunction({
        // 云函数名称
        name: 'reqGradeAPI',
        // 传给云函数的参数
        data: {
            scene: 'GET_USER_GRADE',
            gid: _id,
            gradeDesc: ''
        },
    }).then(res => {
        const { grade } = res.result.data;
        // 从年级信息中，拿出学科信息以及学科下对应的错题数量
        grade.forEach(item => {
            item.children.forEach(item2 => {
                let classTotal = item2.class.length;
                let questionTotal = 0;
                item2.class.forEach(item3 => {
                    questionTotal = questionTotal + item3.qidArr.length;
                })
                gradeArr.push({ classTotal, questionTotal })
            })
        })
        chart = echarts.init(canvas, null, {
            width: width,
            height: height,
            devicePixelRatio: dpr // new
        });
        canvas.setChart(chart);

        var option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                },
                confine: true
            },
            legend: {
                data: ['学科数量', '错题数量']
            },
            grid: {
                left: 20,
                right: 20,
                bottom: 15,
                top: 40,
                containLabel: true
            },
            xAxis: [
                {
                    type: 'value',
                    axisLine: {
                        lineStyle: {
                            color: '#999'
                        }
                    },
                    axisLabel: {
                        color: '#666'
                    }
                }
            ],
            yAxis: [
                {
                    type: 'category',
                    axisTick: { show: false },
                    data: ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级', '初一', '初二', '初三', '高一', '高二', '高三', '大一', '大二', '大三', '大四', '成人'],
                    axisLine: {
                        lineStyle: {
                            color: '#999'
                        }
                    },
                    axisLabel: {
                        color: '#666'
                    }
                }
            ],
            series: [
                {
                    name: '学科数量',
                    type: 'bar',
                    label: {
                        normal: {
                            show: true,
                            position: 'inside'
                        }
                    },
                    data: gradeArr.map(item => item.classTotal),
                    itemStyle: {
                        emphasis: {
                            color: '#37a2da'
                        }
                    }
                },
                {
                    name: '错题数量',
                    type: 'bar',
                    stack: '总量',
                    label: {
                        normal: {
                            show: true
                        }
                    },
                    data: gradeArr.map(item => item.questionTotal),
                    itemStyle: {
                        emphasis: {
                            color: '#32c5e9'
                        }
                    }
                },
            ]
        };
        chart.setOption(option);
        return chart;
    })

}

Page({
    data: {
        ec: {
            onInit: initChart
        },
        showPage: false,
    },

    onLoad: function (options) {
        const that = this;
        setTimeout(() => {
            that.setData({
                showPage: true,
            });
        }, 2000);
    },

    onReady() {
        wx.setNavigationBarTitle({
            title: '各年级下的课程及错题总数'
          })
    }
});
